package com.spring4all.controller;

import com.spring4all.entity.UserEntity;
import com.spring4all.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class HomeController {
	
	@Autowired
    private UserService userService;

    @GetMapping({"/", "/index", "/home"})
    public String root(){
        return "index";
    }

    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @GetMapping("/register")
    public String register(){
        return "register";
    }

    @PostMapping("/register")
    public String doRegister(UserEntity userEntity){
        // 此处省略校验逻辑
        String result = userService.insert(userEntity);
        if (result.equals("found")) {
        	return "redirect:register?found";
		}else if (result.equals("nodata")) {
			return "redirect:register?nodata";
		}else if (result.equals("success")) {
			 return "redirect:register?success";
		}
        return null;
    }

}
